<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class CateController extends Controller
{
    //显示分类添加页面
    public function getAdd(){
        //读取数据
        $cates = DB::table('cates')->get();
        // dd($cates);
        //分配到页面
         return view('admin.cate.add',['cates'=>$cates]);
    }

    /**分类的添加
     * 
     */
   public function postInsert(Request $request)
   {
        //检测
        $this->validate($request,[
                'cname'=>'unique:cates,cname'
            ],[
                'cname.unique'=>'分类名称已经存在'
            ]);
        $data = $request->except(['_token']);
        
        //拼接字段
        //判断是否为顶级分类
        if($data['pid']==0){

            $data['path']= '0';
        }else{//如果不是顶级分类
            //读取父级的分类信息
            $p = DB::table('cates')->where('cid',$data['pid'])->first();
            $data['path'] = $p->path.','.$p->cid;
        }
        $res =  DB::table('cates')->insert($data);
        //提醒
        if($res){
            return redirect('/cate/index')->with('info',"添加成功");
        }else{
            return back()->with('info',"插入失败");
        }
   }
  /**列表显示
   * 
   */
    public function getIndex(Request $request)
    {

        /**读取内容
         * 
         */
     
        $res = DB::table('cates')->select(db::raw('*,concat(path,",",cid) as paths'))->orderBy('paths')->get(2);
        
        foreach($res as $k=>$v){
            //判断
            $total = count(explode(',',$v->path));
            $v->cname = str_repeat('｜-------',$total-1).$v->cname;
        }

        //分配显示
        return view('admin.cate.index',['cates'=>$res,'request'=>$request]);
    }
    
    /**分类的修改页面
     * 
     */
    public function getEdit(Request $request)
    {
        //读取当前的分类信息
        $res =  DB::table('cates')->where('cid',$request->input('cid'))->first();
      //读取所有的类
        $cates = DB::table('cates')->get();
       //显示页面
        return view('admin.cate.edit',['cates'=>$cates,'info'=>$res]);
    }
    /**
     * 分类更新操作
     */
    public function postUpdate(Request $request)
    {
         //检测
        $this->validate($request,[
                'cname'=>'unique:cates,cname'
            ],[
                'cname.unique'=>'分类名称已经存在'
            ]);
        $data = $request->except(['_token']);
        
        //拼接字段
        //判断是否为顶级分类
        if($data['pid']==0){

            $data['path']= '0';
        }else{//如果不是顶级分类
            //读取父级的分类信息
            $p = DB::table('cates')->where('cid',$data['pid'])->first();
            $data['path'] = $p->path.','.$p->cid;
        }
        $res =  DB::table('cates')->where('cid',$data['cid'])->update($data);
        //提醒
        if($res){
            return redirect('/cate/index')->with('info',"修改成功");
        }else{
            return back()->with('info',"修改失败");
        }
    }
    /**
     * 删除
     */
    public function getDelete(Request $request)
    {
        //检测
        $cid = $request->input('cid');
        if(empty($cid)) abort(404);
        //读取信息
        $info =  DB::table('cates')->where('cid',$cid)->first();
        //拼接路径
        $path = $info->path.','.$info->cid;

        //删除
        $res =  DB::table('cates')->where('cid',$cid)->delete();
        $res2 = DB::table('cates')->where('path','like',$path.'%')->delete();
        dd($res2);
    }
}
